package com.example.liang.basamovie.activity;

import android.app.Instrumentation;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.liang.basamovie.R;
import com.example.liang.basamovie.adapter.FindFilmInfoAdapter;
import com.example.liang.basamovie.bean.FindFilmInfo;
import com.example.liang.basamovie.json.FindFilmInfoJson;
import com.example.liang.basamovie.utils.DataPath;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;


/**
 * 电影详情的Activity
 */
public class FilmInfoActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    private String newPath;
    ImageView BackLast,ShareMyFirends,espression;// 返回上一级　　分享朋友普圈　我的表情
    TextView Film_Title;
    EditText editText;
    RelativeLayout SendMessage; //发送我输入的消息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_film_info);
        ShareSDK.initSDK(this);//初始化　ShareSDK
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        //Toast.makeText(getApplication(),id,Toast.LENGTH_LONG).show();
        newPath = DataPath.FILM_INFO_PATH1+id+DataPath.FILM_INFO_PATH2;
        Log.e("path",newPath);
        BackLast= (ImageView)findViewById(R.id.BackLast);
        ShareMyFirends= (ImageView)findViewById(R.id.ShareMyFirends);
        espression= (ImageView)findViewById(R.id.espression);
        SendMessage= (RelativeLayout)findViewById(R.id.SendMessage);
        Film_Title= (TextView) findViewById(R.id.Film_Title);
        editText= (EditText) findViewById(R.id.SendMessage_EditText);

        recyclerView= (RecyclerView) findViewById(R.id.find_FilmInfo_RecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //获取数据
        getData();
        //全部的点击事件
        MyOnClickListener();
    }

    private void getData() {
        OkHttpUtils.get().url(newPath).build().execute(new StringCallback() {
            @Override
            public void onError(Request request, Exception e) {
                Toast.makeText(getApplication(),"获取电影详细信息下载出错",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponse(String response) {
                FindFilmInfo filmInfo = FindFilmInfoJson.getFilmInfo(response);
                FindFilmInfoAdapter adapter = new FindFilmInfoAdapter(filmInfo, FilmInfoActivity.this,Film_Title);
                recyclerView.setAdapter(adapter);


            }
        });
    }
    private void MyOnClickListener() {
        //返回上一级的点击事件
        BackLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplicationContext(),"返回上一级的点击事件",Toast.LENGTH_LONG).show();
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            //Log.e("Exception when sendKeyDownUpSync"e.toString());
                        }
                    }
                }.start();

            }
        });
        //分享朋友圈的点击事件
        ShareMyFirends.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getApplicationContext(),"分享朋友圈的点击事件",Toast.LENGTH_LONG).show();
                showShare();
            }
        });
        //表情的点击事件
        espression.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"表情的点击事件",Toast.LENGTH_LONG).show();
            }
        });
        //发送消息的点击事件
        SendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"发送消息的点击事件",Toast.LENGTH_LONG).show();
            }
        });
        editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                editText.setFocusable(true);
                editText.setFocusableInTouchMode(true);
               //editText.requestFocus();
                return false;
            }
        });
    }
    public void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }


}
